#include "Shutdown_Menu.h"
#include <QEventLoop>


Shutdown_Menu::Shutdown_Menu(QWidget *parent): QWidget(parent)
{
    setupUI();
}

Shutdown_Menu::~Shutdown_Menu()
{
}

void Shutdown_Menu::setMessage(const QString &message) {
    messageLabel->setText(message);
}
void Shutdown_Menu::setIcon(IconType iconType) {
    switch (iconType) {
        case Information:
            iconLabel->setPixmap(QPixmap(":/icons/info.png"));
            break;
        case Warning:
            iconLabel->setPixmap(QPixmap(":/icons/warning.png"));
            break;
        case Critical:
            iconLabel->setPixmap(QPixmap(":/icons/critical.png"));
            break;
        case Question:
            iconLabel->setPixmap(QPixmap(":/icons/question.png"));
            break;
        default:
            iconLabel->setPixmap(QPixmap());
            break;
    }
}
void Shutdown_Menu::addButton(const QString &text, QPushButton **buttonPtr) {
    QPushButton *button = new QPushButton(text, this);
    buttonLayout->addWidget(button);
    connect(button, &QPushButton::clicked, this, &Shutdown_Menu::buttonClicked);
    if (buttonPtr) {
        *buttonPtr = button;
    }
}
void Shutdown_Menu::exec() {
    // 显示消息框
    show();
    // 使消息框模态
    QEventLoop loop;
    connect(this, &Shutdown_Menu::finished, &loop, &QEventLoop::quit);
    loop.exec();
}
void Shutdown_Menu::buttonClicked() {
    emit finished();
    close();
}

void Shutdown_Menu::setupUI() {
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QHBoxLayout *iconLayout = new QHBoxLayout();

    iconLabel = new QLabel(this);
    messageLabel = new QLabel(this);
    buttonLayout = new QHBoxLayout();

    iconLayout->addWidget(iconLabel);
    iconLayout->addWidget(messageLabel);

    mainLayout->addLayout(iconLayout);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);
    // setWindowFlags(Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
    setAttribute(Qt::WA_TranslucentBackground); // 设置背景为半透明
    // 设置无边框和保持顶层
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
}

